package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewsCategoryDaoImpl;

import com.ysd.entity.NewsCategory;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminNewServlet extends BaseServlet {
		//閼惧嘲褰囬崪銊嚄閸掑棛琚�
		//localhost:8080/kouhong/adminNew?reqName=getNews 鐠佸潡妫剁捄顖氱窞

public void getNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		NewsCategoryDaoImpl ncDao = new NewsCategoryDaoImpl();//閸掓稑缂撶�圭偟骞囩猾璇差嚠鐠烇拷
		int page =Integer.parseInt(request.getParameter("page"));
		int size =Integer.parseInt(request.getParameter("size"));
		List<NewsCategory> ncList=ncDao.getNewsCategoryByPage(page, size);//out鐎电钖�
		
		response.setCharacterEncoding("UTF-8");//鐠佸墽鐤嗛崫宥呯安閻ㄥ嫮绱惍浣圭壐瀵拷
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();//閸掓稑缂揋son鐎电钖�
		String res = gson.toJson(ncList);//鐏忓攳clist鏉烆剙瀵叉稉绨�son鐎涙顑佹稉锟�
		out.print(res);//閸濆秴绨�
		out.flush();
		out.close();
	}
//localhost:8080/kouhong/adminNew?reqName=getAllNews
public void getAllNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	NewsCategoryDaoImpl ncDao = new NewsCategoryDaoImpl();//閸掓稑缂撶�圭偟骞囩猾璇差嚠鐠烇拷
	
	List<NewsCategory> ncList=ncDao.getNewsCategory();//out鐎电钖�
	
	response.setCharacterEncoding("UTF-8");//鐠佸墽鐤嗛崫宥呯安閻ㄥ嫮绱惍浣圭壐瀵拷
	PrintWriter out = response.getWriter();

	Gson gson = new Gson();//閸掓稑缂揋son鐎电钖�
	String res = gson.toJson(ncList.size());//鐏忓攳clist鏉烆剙瀵叉稉绨�son鐎涙顑佹稉锟�
	out.print(res);//閸濆秴绨�
	out.flush();
	out.close();
}

	//localhost:8080/kouhong/adminNew?reqName=delNews 鐠佸潡妫剁捄顖氱窞
	public void delNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("newsCategoryId"));//閹恒儱褰堥崜宥呭酱
		NewsCategoryDaoImpl ncDao = new NewsCategoryDaoImpl();//閸掓稑缂撶�圭偟骞囩猾璇差嚠鐠烇拷
		int n =ncDao.delNewsCateById(cId);
		response.setCharacterEncoding("UTF-8");//鐠佸墽鐤嗛崫宥呯安閻ㄥ嫮绱惍浣圭壐瀵拷
		PrintWriter out = response.getWriter();
		
		
		
		if (n>0) {
			out.print(Result.toClient(true, "鍒犻櫎鎴愬姛", ""));
		} else {
			out.print(Result.toClient(false, "鍒犻櫎澶辫触", ""));
		}
		
		out.flush();
		out.close();
	}
	
	//localhost:8080/kouhong/adminNew?reqName=addNews 鐠佸潡妫剁捄顖氱窞
		public void addNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			NewsCategoryDaoImpl ncDao = new NewsCategoryDaoImpl();
			String newsCategoryName =request.getParameter("newsCategoryName");
			
			if(newsCategoryName!=null && !newsCategoryName.equals("")){
				
				List<NewsCategory> ncList=ncDao.getNewsCategoryName(newsCategoryName);
				if(ncList.size()==0) {
					NewsCategory nc = new NewsCategory(0,newsCategoryName);
					int n =ncDao.addCategory(nc);
					if (n>0) {
						out.print(Result.toClient(true, "娣诲姞鎴愬姛", ""));
					} else {
						out.print(Result.toClient(false, "娣诲姞澶辫触", ""));
					}
					
				}else {
					out.print(Result.toClient(false, "鍒嗙被鍚嶇О宸插瓨鍦�", ""));
				}
			}
			
			
			out.flush();
			out.close();
		}
		
		
		//localhost:8080/kouhong/adminNew?reqName=getNewsById鐠佸潡妫剁捄顖氱窞
				public void getNewsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					response.setCharacterEncoding("UTF-8");
					PrintWriter out = response.getWriter();
					NewsCategoryDaoImpl ncDao = new NewsCategoryDaoImpl();
					int cateId = 0;
					if(request.getParameter("cId")!=null && !request.getParameter("cId").equals("")) {
						cateId = Integer.parseInt(request.getParameter("cId"));
					}
				 
					
					NewsCategory nc = ncDao.getNewsCategorById(cateId);
					System.out.println(nc);
					Gson gson = new Gson();
					String res = gson.toJson(nc);
					out.print(res);
					
					out.flush();
					out.close();
				}
				
				//localhost:8080/kouhong/adminNew?reqName=updateNewsById鐠佸潡妫剁捄顖氱窞
				public void updateNewsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					response.setCharacterEncoding("UTF-8");
					PrintWriter out = response.getWriter();
					NewsCategoryDaoImpl ncDao = new NewsCategoryDaoImpl();
					
					int newsCategoryId =Integer.parseInt(request.getParameter("cId"));
					
					String newsCategoryName =request.getParameter("cateName");
					NewsCategory nc = new NewsCategory(newsCategoryId,newsCategoryName);
					int n =ncDao.updateNewsCategoryById(nc);
					if (n>0) {
						out.print(Result.toClient(true, "修改成功", ""));
					} else {
						out.print(Result.toClient(false, "修改失敗", ""));
					}
					
					
					out.flush();
					out.close();
				}
				//localhost:8080/kouhong/adminNew?reqName=deleteNewsByIds鐠佸潡妫剁捄顖氱窞
				public void deleteNewsByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					response.setCharacterEncoding("UTF-8");
					PrintWriter out = response.getWriter();
					NewsCategoryDaoImpl ncDao = new NewsCategoryDaoImpl();
					String ids = request.getParameter("ids");
					int n=ncDao.delMessageByIds(ids);
					
					if (n>0) {
						out.print(Result.toClient(true, "删除成功", ""));
					} else {
						out.print(Result.toClient(false, "删除失败", ""));
					}
					
					
					out.flush();
					out.close();
				}
				
}